﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GrayParrot.Extensions.Strings;

namespace GrayParrot.Web.Formly
{
    public class EventsConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return (objectType == typeof(FieldItem));
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            FieldItem obj = (FieldItem)value;

            if ((obj.TemplateOptions == null) && ((obj.Childs != null) && (obj.Childs.Count() > 0)))
            {
                if ((obj.Childs != null) && (obj.Childs.Count > 0))
                {
                    JObject jo = new JObject();
                    JArray m_childItem = new JArray();

                    foreach (FieldItem fi in obj.Childs)
                    {
                        TemplateOptions m_temp = fi.TemplateOptions;

                        // Create a JObject from the document, respecting existing JSON attribs
                        JObject jo1 = JObject.FromObject(fi);
                        JObject joTO = JObject.FromObject(m_temp);

                        foreach (EventCallback ec in m_temp.Events)
                        {
                            joTO.Add(ec.Evento.ToString().FirstCharacterToLower(), JToken.FromObject(ec.Callback));
                        }

                        jo1["templateOptions"] = joTO;

                        m_childItem.Add(jo1);
                    }

                    jo["className"] = "row";
                    jo["fieldGroup"] = m_childItem;
                    jo.WriteTo(writer);
                }
                else
                {
                    JObject jo = JObject.FromObject(value);
                    TemplateOptions m_temp = obj.TemplateOptions;

                    JObject joTO = JObject.FromObject(m_temp);

                    foreach (EventCallback ec in m_temp.Events)
                    {
                        joTO.Add(ec.Evento.ToString().FirstCharacterToLower(), JToken.FromObject(ec.Callback));
                    }

                    jo["templateOptions"] = joTO;
                    jo.WriteTo(writer);
                }
            }
            else
            {
                if ((obj.TemplateOptions != null) && (obj.Childs == null))
                {
                    if ((obj.TemplateOptions != null) && ((obj.TemplateOptions.Events != null) && (obj.TemplateOptions.Events.Count() > 0)))
                    {
                        JObject jo = JObject.FromObject(value);
                        TemplateOptions m_temp = obj.TemplateOptions;

                        JObject joTO = JObject.FromObject(m_temp);

                        foreach (EventCallback ec in m_temp.Events)
                        {
                            joTO.Add(ec.Evento.ToString().FirstCharacterToLower(), JToken.FromObject(ec.Callback));
                        }

                        jo["templateOptions"] = joTO;
                        jo.WriteTo(writer);
                    }
                    else
                    {
                        JObject jo = JObject.FromObject(value);
                        jo.WriteTo(writer);
                    }
                }
                else
                {
                    JObject jo = new JObject();
                    jo.WriteTo(writer);
                }
            }
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }
}
